﻿using AutoMapper;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SXICE.AdminAPI.Filters;
using SXICE.AdminAPI.Models;
using SXICE.AdminAPI.Utilities;
using SXICE.Core.Domains;
using SXICE.Core.Helpers;
using SXICE.Core.Models;
using SXICE.Core.Models.DTOs;
using SXICE.Core.Models.Search;
using SXICE.IService;
using SXICE.IService.Models;

namespace SXICE.AdminAPI.Controllers
{
    /// <summary>
    /// 医生控制器
    /// </summary>

    [Route("")]
    [Authorize]
    public class DoctorController : ControllerBase
    {
        private readonly IMapper mapper;
        private readonly IDoctorService doctorService;
        private readonly JwtSettings jwtSettings;

        public DoctorController(IMapper mapper, IDoctorService userService, JwtSettings jwtSettings)
        {
            this.mapper = mapper;
            this.doctorService = userService;
            this.jwtSettings = jwtSettings;
        }

        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="doctor">用户信息对象</param>
        /// <returns></returns>
        [HttpPost]
        [Rights("AddDoctor")]
        public ReturnResult<bool> Register([FromBody] DoctorDTO doctor)
        {
            return doctorService.Register(mapper.Map<Doctor>(doctor));
        }

        /// <summary>
        /// 开始
        /// </summary>
        /// <param name="doctor">用户登陆信息</param>
        /// <returns>结果</returns>
        [HttpPost]
        [AllowAnonymous]
        public ReturnResult<DoctorDTO> LogIn([FromBody] DoctorDTO doctor)
        {
            var result = doctorService.GetUser(doctor);
            if (!result.Success)
                return result;
            var token = JwtHelper.GenerateToken(new DoctorClaimsPrincipal
            {
                UserId = result.Result.Id,
                RoleId = result.Result.RoleId
            }, jwtSettings);
            if (jwtSettings.AuthorizationType == AuthorizationTypeEnum.Cookie)
                HttpContext.Response.Cookies.Append("Authorization", token, new Microsoft.AspNetCore.Http.CookieOptions
                {
                    HttpOnly = true
                });
            result.Result.Password = token;
            return result;
        }

        /// <summary>
        ///获取所有信息
        /// </summary>
        /// <param name="pageListQuery">查询条件</param>
        /// <returns></returns>
        [HttpPost]
        [Rights("SearchDoctor")]
        public ReturnPageListResult<DoctorDTO> GetAll([FromBody] PageListQuery<RecordSearchModel> pageListQuery)
        {
            return doctorService.Search(pageListQuery);
        }

        /// <summary>
        /// 根据Id更新用户信息
        /// </summary>
        /// <param name="doctor">用户</param>
        /// <returns></returns>
        [HttpPost]
        [Rights("EditDoctor")]
        public ReturnResult<bool> Update([FromBody] DoctorDTO doctor)
        {
            return doctorService.Update(mapper.Map<Doctor>(doctor));
        }

        /// <summary>
        /// 更新角色
        /// </summary>
        /// <param name="doctor">要更新的内容</param>
        /// <returns>结果</returns>
        [HttpPut]
        [Rights("AssignRole")]
        public ReturnResult<bool> UpdateRole([FromBody] DoctorDTO doctor)
        {
            return doctorService.UpdateRole(mapper.Map<Doctor>(doctor));
        }

        /// <summary>
        /// 根据Id删除用户信息
        /// </summary>
        /// <param name="id">用户Id</param>
        /// <param name="state">状态</param>
        /// <returns></returns>
        [HttpDelete]
        [Rights("DeleteDoctor")]
        public ReturnResult<bool> Delete(int id, bool state)
        {
            if (id == WorkContext.DoctorClaims.UserId)
                return ReturnResult<bool>.GetResult(1111, "自己不能更改自己账号的状态");
            return doctorService.Delete(id, state);
        }

        /// <summary>
        /// 修改真实姓名
        /// </summary>
        /// <param name="doctor">要修改的医生</param>
        /// <returns>结果</returns>
        [HttpPut]
        public ReturnResult<bool> UpdateRealName([FromBody] Doctor doctor)
        {
            doctor.Id = WorkContext.DoctorClaims.UserId;
            return doctorService.Update(doctor);
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="changePasswordDTO">要修改密码的信息</param>
        /// <returns>结果</returns>
        [HttpPut]
        public ReturnResult<bool> UpdatePassword([FromBody] ChangePasswordDTO changePasswordDTO)
        {
            changePasswordDTO.Id = WorkContext.DoctorClaims.UserId;
            return doctorService.UpdatePassword(changePasswordDTO);
        }
    }
}
